package ControlLayer;
import ModelLayer.*;
import DBLayer.*;
import java.util.ArrayList;

public class SupplierCustomerCtr
{
	public SupplierCustomerCtr()
	{
		
	}
	
	public ArrayList<Supplier> findAllSuppliers()
	{
		IFDBSupplier dbSup = new DBSupplier();
		ArrayList<Supplier> allSup = new ArrayList<Supplier>();
		allSup = dbSup.getAllSuppliers(false);
		return allSup;
	}
	
	public ArrayList<Customer> findAllCustomers()
	{
		IFDBCustomer dbCus = new DBCustomer();
		ArrayList<Customer> allCus = new ArrayList<Customer>();
		allCus = dbCus.getAllCustomers(false);
		return allCus;
	}
	
	public Supplier searchByName(String name)
	{
		IFDBSupplier dbSup = new DBSupplier();
		return dbSup.searchByName(name, true);
	}
	
	public Customer findByName(String name)
	{
		IFDBCustomer dbCus = new DBCustomer();
		return dbCus.searchCustomerName(name, true);
	}
	
	public Supplier findSupplierByID(int id)
	{
		IFDBSupplier dbSup = new DBSupplier();
		return dbSup.findSupplierByID(id, true);
	}
	
	public Customer findByID(int id)
	{
		IFDBCustomer dbCus = new DBCustomer();
		return dbCus.findCustomer(id, true);
	}
	
	public int updateSupplier(String name, String address, String country, String phoneNo, String email)
	{
		IFDBSupplier dbSup = new DBSupplier();
		Supplier sup = new Supplier();
		sup.setName(name);
		sup.setAddress(address);
		sup.setCountry(country);
		sup.setPhoneNo(phoneNo);
		sup.setEmail(email);
		return dbSup.updateSupplier(sup);
	}
	
	public int updateCustomer(String name, String address, String city, String phoneNo, String mail)
	{
		IFDBCustomer dbCus = new DBCustomer();
		Customer cus = new Customer();
		cus.setName(name);
		cus.setAddress(address);
		cus.setCity(city);
		cus.setPhoneNo(phoneNo);
		cus.setMail(mail);
		return dbCus.updateCustomer(cus);
	}
	
	public void insertNewSupplier(String name, String address, String country, String phoneNo, String email)
	{
		Supplier supObj = new Supplier();
		supObj.setName(name);
		supObj.setAddress(address);
		supObj.setCountry(country);
		supObj.setPhoneNo(phoneNo);
		supObj.setEmail(email);
		
		try
		{
			DbConnection.startTransaction();
			DBSupplier dbSup = new DBSupplier();
			dbSup.insertNewSupplier(supObj);
			DbConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DbConnection.rollbackTransaction();
		}
	}
	
	public void insertNewCustomer(String name, String address, String city, String phoneNo, String mail)
	{
		Customer cusObj = new Customer();
		cusObj.setName(name);
		cusObj.setAddress(address);
		cusObj.setCity(city);
		cusObj.setPhoneNo(phoneNo);
		cusObj.setMail(mail);
		
		try
		{
			DbConnection.startTransaction();
			DBCustomer dbCus = new DBCustomer();
			dbCus.insertCustomer(cusObj);
			DbConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DbConnection.rollbackTransaction();
		}
	}
}
